package com.dc.chat.app.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dc.chat.app.biz.BackBiz;
import com.dc.chat.app.service.mp.*;
import com.dc.chat.entity.dao.*;
import com.dc.chat.entity.to.ResponseBean;
import com.dc.chat.entity.to.back.AddSensitiveReq;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class BackBizImpl implements BackBiz {
    @Resource
    AdminService adminService;
    @Resource
    UserService userService;
    @Resource
    FeedBackService feedBackService;
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    CommonMessageService commonMessageService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    SensitiveService.SensitiveMapper sensitiveMapper;
    @Override
    public ResponseBean<String> login(String username, String password) {
        Admin admin=adminService.selectByUsernameAndPassword(username,password);
        if(admin==null){
            return ResponseBean.failed("用户名或密码错误");
        }else{
            String s = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(s,"admin",60, TimeUnit.MINUTES);
            return ResponseBean.success("登录成功",s);
        }
    }

    @Override
    public ResponseBean<Integer> getOnlineNum() {
        Map login = redisTemplate.opsForHash().entries("login");
        int size = login.size();
        return ResponseBean.success("查询成功",size);
    }

    @Override
    public ResponseBean<List<User>> getEditUserList() {
        List<User> users = userService.getBaseMapper().selectList(null);
        Map usernameTokenMap = redisTemplate.opsForHash().entries("login");
        Set userSet = usernameTokenMap.keySet();
        for (User user : users) {
            if(userSet.contains(user.getUserName())){
                user.setOnline(true);
            }else{
                user.setOnline(false);
            }
            user.setPassword(null);
        }
        return ResponseBean.success("查询成功",users);
    }

    @Override
    public ResponseBean<List<FeedBack>> getFeedBack() {
        LambdaQueryWrapper<FeedBack> wrapper = Wrappers.lambdaQuery(new FeedBack());
        wrapper.orderByDesc(FeedBack::getCreateTime);
        List<FeedBack> feedBacks = feedBackService.getBaseMapper().selectList(wrapper);
        return ResponseBean.success("查询成功",feedBacks);
    }

    @Override
    public ResponseBean markFeedBack(Integer id) {
        FeedBack byId = feedBackService.getById(id);
        if(byId==null){
            return ResponseBean.failed("没有此条记录");
        }else{
            byId.setStatus(1);
            feedBackService.updateById(byId);
            //通知各个管理员  该刷新界面了
            Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
            for (String adminToken : adminTokens) {
                Map map=new HashMap();
                map.put("refreshFeedBack",true);
                simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
            }
            return ResponseBean.success("操作成功",null);
        }

    }

    @Override
    public ResponseBean deleteFeedBack(Integer id) {
        FeedBack byId = feedBackService.getById(id);
        if(byId==null){
            return ResponseBean.failed("没有此条记录");
        }else{
            feedBackService.removeById(id);
            //通知各个管理员  该刷新界面了
            Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
            for (String adminToken : adminTokens) {
                Map map=new HashMap();
                map.put("refreshFeedBack",true);
                simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
            }
            return ResponseBean.success("操作成功",null);
        }
    }

    @Override
    public ResponseBean<List<CommonMessage>> getCommonMessage() {
        LambdaQueryWrapper<CommonMessage> wrapper = Wrappers.lambdaQuery(new CommonMessage());
        wrapper.orderByDesc(CommonMessage::getCreateTime);
        List<CommonMessage> commonMessages = commonMessageService.getBaseMapper().selectList(wrapper);
        return ResponseBean.success("查询成功",commonMessages);
    }

    @Override
    public ResponseBean removeCommonMessage(Integer id) {
        boolean b = commonMessageService.removeById(id);
        if(b){
            //通知所用人  普通用户和管理员 刷新聊天
            Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
            Map map=new HashMap();
            map.put("refreshCommonMessage",true);
            for (String adminToken : adminTokens) {
                simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
            }
            Map userTokenMap = redisTemplate.opsForHash().entries("login");
            Collection<String> userTokens = userTokenMap.values();
            for (String userToken : userTokens) {
                simpMessagingTemplate.convertAndSend("/u/"+userToken,"",map);
            }
            return ResponseBean.success("操作成功",null);
        }else{
            return ResponseBean.failed("操作失败");
        }
    }

    @Override
    public void addSensitive(AddSensitiveReq req) {
        Sensitive sensitive=new Sensitive(  );
        sensitive.setContent(req.getContent());
        sensitive.setCreateTime(LocalDateTime.now());
        sensitive.setType(req.getType());
        sensitiveMapper.insert(sensitive);
        Set<String> adminToken = redisTemplate.opsForSet().members("admin");
        Map map=new HashMap();
        map.put("refreshSensitive",true);
        for (String token : adminToken) {
            simpMessagingTemplate.convertAndSend("/back/"+token,"",map);
        }

    }
}
